package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.net.URISyntaxException;

/**
 * @Program: WebServerTomcat
 * @Author: Evan
 * @Date: 2022-05-17 18:11
 * @Description: 转发器
 * @Version: V1.0
 */
public class DispatcherServlet {
    private static File rootDir;
    private static File staticDir;
    static {
        try {
            rootDir = new File(
                    ClientHandler.class.getClassLoader().getResource(".").toURI()
            );
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
     public void service(HttpServletRequest request, HttpServletResponse response){
         String path = request.getUrl();

         File file = new File(staticDir,path);
         if (file.isFile()){
             response.setEntity(file);
         }else {
             response.setStatusCode(404);
             response.setStatusReason("NotFound");
             response.setEntity(new File(staticDir,"./root/404.html"));
         }
     }
}
